using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Skill : LoadableJSONObject{
	public string stat = "Strength";
	
	public bool armorCheckPenalty = true;
	
	public bool trainedOnly = false;
	
	public int rank = 0;
	
	public int bonus = 0;
	
	public int totalbonus
	{
		get
		{
			int b = SheetManager.activeCharacter.GetBuffMods("Skill", name).AddUpTotals();
			
			if(armorCheckPenalty)	
				b += SheetManager.activeCharacter.ArmorCheckPenalty;
			
			if(SheetManager.activeCharacter.IsClassSkill(name) && rank > 0)
			{
				b += 3;
			}
			
			return b;
		}
	}
	
	public int TotalValue
	{
		get
		{
			return statBonus + rank + totalbonus;
		}
	}
	
	public int statBonus
	{
		get
		{
			int _statBonus;
			
			if(stat == "Strength")
			{
				_statBonus = SheetManager.activeCharacter.StrengthModifier;
			}
			else if(stat == "Dexterity")
			{
				_statBonus = SheetManager.activeCharacter.DexterityModifier;
			}
			else if(stat == "Constitution")
			{
				_statBonus = SheetManager.activeCharacter.ConstitutionModifier;
			}
			else if(stat == "Intelligence")
			{
				_statBonus = SheetManager.activeCharacter.IntelligenceModifier;
			}
			else if(stat == "Wisdom")
			{
				_statBonus = SheetManager.activeCharacter.WisdomModifier;
			}
			else
			{
				_statBonus = SheetManager.activeCharacter.CharismaModifier;
			}
			
			return _statBonus;
		}
	}
	
	public void RollSkill()
	{
		SheetManager.activeCharacter.SetMathParserStats();
		
		List<RollDefinition> rolls = new List<RollDefinition>();
		rolls.Add(new RollDefinition());

		RollDefinition SB = new RollDefinition();
		SB.dieCount = 0;
		SB.bonusAmount = statBonus;
		SB.type = "Stat Bonus";
		rolls.Add(SB);
		
		SB = new RollDefinition();
		SB.dieCount = 0;
		SB.bonusAmount = totalbonus;
		SB.type = "Skill Bonuses";
		rolls.Add(SB);
		
		SB = new RollDefinition();
		SB.dieCount = 0;
		SB.bonusAmount = rank;
		SB.type = "Rank";
		rolls.Add(SB);
		
		GameReference.reference.StartCoroutine(HelperFunctions.RollWindow(rolls, name + " Check"));
	}
	
	public Skill()
	{
	}
	
	public override void Load(string json, string file)
	{
		fileName = file;
		if(json != null)
		{
			Dictionary<string,object> data = MiniJSON.Json.Deserialize(json) as Dictionary<string,object>;
			
			name = data["name"] as string;
			description = data["description"] as string;
			stat = data["stat"] as string;
			
			armorCheckPenalty = bool.Parse(data["armorCheckPenalty"] as string);
			trainedOnly = bool.Parse(data["trainedOnly"] as string);
			
			rank = int.Parse(data["rank"] as string);
			bonus = int.Parse(data["bonus"] as string);
		}
	}
	
	public override string ToJSON()
	{
		Dictionary<string,string> data = new Dictionary<string, string>();
		data.Add("name", name);
		data.Add("description", description.ToString());
		data.Add("stat", stat.ToString());
		data.Add("armorCheckPenalty", armorCheckPenalty.ToString());
		data.Add("rank", rank.ToString());
		data.Add("bonus", bonus.ToString());
		data.Add("trainedOnly", trainedOnly.ToString());
		
		return MiniJSON.Json.Serialize(data);
	}
	
	public Skill DeepCopy()
	{
		Skill a = new Skill();
		a.Load(ToJSON(), fileName);
		return a;
	}
}
